﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class LocationService
{

   

    
    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string saveLocation(string LocationName, string LocationAddress, string LocationCountry, string LocationState, string LocationCity)
    {
        try
        {
            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();

            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "MAS_pLocationInsert";
            cmd.Parameters.Add("@LocationName", SqlDbType.VarChar).Value = LocationName;
            cmd.Parameters.Add("@LocationAddress", SqlDbType.VarChar).Value = LocationAddress;
            cmd.Parameters.Add("@LocationCountry", SqlDbType.VarChar).Value = LocationCountry;
            cmd.Parameters.Add("@LocationState", SqlDbType.VarChar).Value = LocationState;
            cmd.Parameters.Add("@LocationCity", SqlDbType.VarChar).Value = LocationCity;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            con.Open();
            sda.Fill(ds);
            con.Close();
            return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "', Confirmation :'" + ds.Tables[0].Rows[0]["Confirmation"] + "' }";


        }
        catch (Exception)
        {
            return "{Message: 'Failed' ,Status :'2' }";
        }
        
    }



    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string modifyLocation(int LocationID, string LocationName, string LocationAddress, string LocationCountry, string LocationState, string LocationCity)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pLocationUpdate";
        cmd.Parameters.Add("@LocationID", SqlDbType.Int).Value = LocationID;
        cmd.Parameters.Add("@LocationName", SqlDbType.VarChar).Value = LocationName;
        cmd.Parameters.Add("@LocationAddress", SqlDbType.VarChar).Value = LocationAddress;
        cmd.Parameters.Add("@LocationCountry", SqlDbType.VarChar).Value = LocationCountry;
        cmd.Parameters.Add("@LocationState", SqlDbType.VarChar).Value = LocationState;
        cmd.Parameters.Add("@LocationCity", SqlDbType.VarChar).Value = LocationCity;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] + "'}";
    }

    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string removeLocation(int LocationID)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pLocationDelete";
        cmd.Parameters.Add("@LocationID", SqlDbType.Int).Value = LocationID;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{Message: '" + ds.Tables[0].Rows[0]["Message"] +  "'}";
    }


    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string getLocation(int LocationID)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pFetchLocation";
        cmd.Parameters.Add("@p_LocationId", SqlDbType.Int).Value = LocationID;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{LocationAddress:'" + ds.Tables[0].Rows[0]["LocationAddress"] + "',LocationCity: '" + ds.Tables[0].Rows[0]["LocationCity"] +
                "',LocationCountry:'" + ds.Tables[0].Rows[0]["LocationCountry"] + "',LocationState:'" + ds.Tables[0].Rows[0]["LocationState"] +
                "',LocationName:'" + ds.Tables[0].Rows[0]["LocationName"] + "'}";
    }


    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json)]
    public string fetchLocation(int LocationID)
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "MAS_pFetchLocation";
        cmd.Parameters.Add("@p_LocationId", SqlDbType.Int).Value = LocationID;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        con.Open();
        sda.Fill(ds);
        con.Close();
        return "{LocationAddress:'" + ds.Tables[0].Rows[0]["LocationAddress"] + "',LocationCity: '" + ds.Tables[0].Rows[0]["LocationCity"] +
                "',LocationCountry:'" + ds.Tables[0].Rows[0]["LocationCountry"] + "',LocationState:'" + ds.Tables[0].Rows[0]["LocationState"] +
                "',LocationName:'" + ds.Tables[0].Rows[0]["LocationName"] + "'}";
    }


}
